package com.example.learnloadmanager;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements LoaderCallbacks<Cursor> {

    private LoaderManager loaderManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "a");
        getContentResolver().insert(MyContentProvider.MyContentProvider_Uri,
                contentValues);

        ContentValues contentValues1 = new ContentValues();
        contentValues1.put("name", "b");
        getContentResolver().insert(MyContentProvider.MyContentProvider_Uri,
                contentValues1);

        ContentValues contentValues2 = new ContentValues();
        contentValues2.put("name", "c");
        getContentResolver().insert(MyContentProvider.MyContentProvider_Uri,
                contentValues2);

        ContentValues contentValues3 = new ContentValues();
        contentValues3.put("name", "d");
        getContentResolver().insert(MyContentProvider.MyContentProvider_Uri,
                contentValues3);

        loaderManager = this.getLoaderManager();
        loaderManager.initLoader(1000, null, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // TODO Auto-generated method stub
        CursorLoader loader = new CursorLoader(this,
                MyContentProvider.MyContentProvider_Uri, null, null, null, null);
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // TODO Auto-generated method stub
        ArrayList<String> arrayList = new ArrayList<String>();
        while (data.moveToNext()) {
            String i = data.getString((data.getColumnIndex("name")));
            arrayList.add(i);
        }
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println("name= " + arrayList.get(i));
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // TODO Auto-generated method stub

    }
}
